package com.portalEsporteClubeBahia.app.bo;

import java.util.List;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.dao.UserDAO;
import com.portalEsporteClubeBahia.app.util.Security;
import com.portalEsporteClubeBahia.app.vo.UserVO;

public class UserBO {
	
	private UserDAO userDAO;
	
	public UserBO() throws ApplicationException{
		try {
			userDAO = new UserDAO();
		} catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public UserVO findByUserPassword(UserVO user) throws ApplicationException{
		
		if(user.getUser()==null || user.getUser().equalsIgnoreCase("")){
			throw new ApplicationException("Missing USER!");
		}
		
		if(user.getPassword()==null || user.getPassword().equalsIgnoreCase("")){
			throw new ApplicationException("Missing PASSWORD!");
		}
		
		user.setPassword(Security.criptography(user.getPassword()));
		
		try{
			return userDAO.findByUserPassword(user);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
		
	}
	
	public UserVO findByUser(UserVO user) throws ApplicationException{
		
		if(user.getUser()==null || user.getUser().equalsIgnoreCase("")){
			throw new ApplicationException("Missing USER!");
		}
		
		try{
			return userDAO.findByUser(user);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
		
	}
	
	public List<UserVO> findByFilter(UserVO user) throws ApplicationException{
		
		try{
			return userDAO.findByFilter(user);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
		
	}
	
	public void insert(UserVO user) throws ApplicationException{
		
		if(user.getName() == null || user.getName().equalsIgnoreCase("")){
			throw new ApplicationException("Missing NAME!");
		}
		
		if(user.getUser()==null || user.getUser().equalsIgnoreCase("")){
			throw new ApplicationException("Missing USER!");
		}
		
		user.setPassword(Security.criptography(user.getUser()+"@01"));
		
		try{
			userDAO.insert(user);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
		
	}
	
	public void update(UserVO user) throws ApplicationException{
		
		if(user.getId()==null){
			throw new ApplicationException("Missing ID!");
		}
		
		if(user.getName() == null || user.getName().equalsIgnoreCase("")){
			throw new ApplicationException("Missing NAME!");
		}
		
		if(user.getUser()==null || user.getUser().equalsIgnoreCase("")){
			throw new ApplicationException("Missing USER!");
		}
		
		if(user.getPassword()==null || user.getPassword().equalsIgnoreCase("")){
			throw new ApplicationException("Missing PASSWORD!");
		}
		
		user.setPassword(Security.criptography(user.getPassword()));
		
		try{
			userDAO.update(user);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
		
	}
	
	public void updatePassword(UserVO user) throws ApplicationException{
		
		if(user.getId()==null){
			throw new ApplicationException("Missing ID!");
		}
		
		if(user.getPassword()==null || user.getPassword().equalsIgnoreCase("")){
			throw new ApplicationException("Missing PASSWORD!");
		}
		
		user.setPassword(Security.criptography(user.getPassword()));
		
		try{
			userDAO.updatePassword(user);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
		
	}
	
	public void delete(UserVO user) throws ApplicationException{
		
		if(user.getId()==null){
			throw new ApplicationException("Missing ID!");
		}
		
		try{
			userDAO.delete(user);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
		
	}
	
	public List<UserVO> findAll() throws ApplicationException{

		try{
			return userDAO.findAll();
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
		
	}
	
	public UserVO findByPrimaryKey(UserVO user) throws ApplicationException{
		
		if(user.getId()==null){
			throw new ApplicationException("Missing ID!");
		}
		
		try{
			return userDAO.findByPrimaryKey(user);
		}catch(ApplicationException e){
			throw new ApplicationException(e);
		}
		
	}

}
